//
//  SPlayerMultipleSheet.swift
//  ngw
//
//  Created by y s on 2022/11/7.
//  Copyright © 2022 taojinzhe. All rights reserved.
//

import UIKit


@objcMembers public class SPlayerMultipleSheet: UIView {
    
    let arrText: Array = ["0.5X","1.0X","1.25X","1.5X","2.0X"]
    let arrNum: Array<Float> = [0.5, 1.0, 1.25, 1.5, 2.0]
    var vSuper: UIView?
    var isFull: Bool = false
    
    public var changeMulpleBlock: ((_ content: Float) -> ())?
    
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    func setUI() {
        backgroundColor = SUIUtil.colorOf(0x000000, alpha: 0.75)
        clipsToBounds = true
        layer.cornerRadius = SFitUtil.fit(2)
        var btnPre: UIButton? = nil
        for i in 0...arrText.count-1 {
            let btnText = SDataUtil.stringOf(arrText[i])
            let btn = UIButton(type: .custom)
            btn.setTitle(btnText, for: .normal)
            btn.setTitleColor(STheme.D9, for: .normal)
            btn.setTitleColor(STheme.D12, for: .selected)
            btn.titleLabel?.font = SFitUtil.fitFont(12)
            btn.addTarget(self, action: #selector(btnClickAction(_ :)), for: UIControl.Event.touchUpInside)
            btn.tag = 200+i
            addSubview(btn)
            btn.snp.makeConstraints({ make in
                make.left.right.equalToSuperview()
                if btnPre != nil {
                    make.top.equalTo(btnPre!.snp.bottom)
                    make.height.equalTo(btnPre!)
                }else{
                    make.top.equalToSuperview()
                }
                if i == arrText.count-1 {
                    make.bottom.equalToSuperview()
                }
            })
            if i == 1 {
                btn.isSelected = true
            }
            btnPre = btn
        }
    }
    
    public func show(_ view: UIView, isFull: Bool) {
        vSuper = view
        if vSuper == nil {
            return
        }
        self.isFull = isFull
        isHidden = false
        vSuper?.isHidden = false
        let vSize = SPlayerMultipleSheet.getViewSize(isFull)
        frame = CGRect(x: 0, y: vSize.height, width:vSize.width , height: 0)
        vSuper?.addSubview(self)
        
        let arrTemp = subviews
        for i in 0...arrTemp.count-1 {
            guard let btn = arrTemp[i] as? UIButton else {
                return
            }
            if isFull {
                btn.titleLabel?.font = SFitUtil.fitFont(14)
            }else{
                btn.titleLabel?.font = SFitUtil.fitFont(12)
            }
        }
        UIView.animate(withDuration: 0.3) {
            self.frame = CGRect(x: 0, y: 0, width: vSize.width, height: vSize.height)
        }
    }
    public func hide() {
        UIView.animate(withDuration: 0.3) {
            let vSize = SPlayerMultipleSheet.getViewSize(self.isFull)
            self.frame = CGRect(x: 0, y: vSize.height, width: vSize.width, height: vSize.height)
        }completion: { make in
            self.isHidden = true
            self.vSuper?.isHidden = true
        }
    }
    
    
    public func resetDeafultStatus(){
        let arrView = subviews
        for i in 0...arrView.count-1 {
            let view = subviews[i]
            if let btn = view as? UIButton {
                if i == 1 {
                    btn.isSelected = true
                }else{
                    btn.isSelected = false
                }
            }
        }
    }
    
    
    public class func getViewSize(_ isFull: Bool) -> CGSize {
        var vHeight = SFitUtil.fit(25)*5
        let w = SFitUtil.fit(56)
        if (isFull){
            vHeight = SFitUtil.fit(36)*5
        }
        return CGSizeMake(w, vHeight)
    }
    
    
    func btnClickAction(_ sender: UIButton) {
        let arrView = subviews
        for i in 0...arrView.count-1 {
            let view = subviews[i]
            if let btn = view as? UIButton {
                btn.isSelected = false
            }
        }
        sender.isSelected = true
        if changeMulpleBlock != nil {
            changeMulpleBlock!(arrNum[sender.tag-200])
        }
        hide()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
